/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.business;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.DateUtils;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.business.ActivityDao;
import com.neusoft.lohas.modules.ivm.dao.business.ActivityTimeDao;
import com.neusoft.lohas.modules.ivm.entity.business.Activity;
import com.neusoft.lohas.modules.ivm.entity.business.ActivityProduct;
import com.neusoft.lohas.modules.ivm.entity.business.ActivityTime;

/**
 * 活动信息Service
 * @author Jackie.Liu
 * @version 2015-01-30
 */
@Component
@Transactional(readOnly = true)
public class ActivityService extends BaseService {

	@Autowired
	private ActivityDao activityDao;
	@Autowired
	private ActivityTimeDao activityTimeDao;
	
	public Activity get(String id) {
		return activityDao.get(id);
	}
	
	public Page<Activity> find(Page<Activity> page, Activity activity) {
		DetachedCriteria dc = activityDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(activity.getName())){
			dc.add(Restrictions.like("name", "%"+activity.getName()+"%"));
		}
		if (StringUtils.isNotBlank(activity.getType())) {
			dc.add(Restrictions.like("type", activity.getType()));
		}
		dc.add(Restrictions.eq(Activity.FIELD_DEL_FLAG, Activity.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", activity.getMerchant().getId()));
		return activityDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Activity activity) {
		activityDao.clear();
		activityDao.save(activity);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		activityDao.deleteById(id);
	}
	
	public Activity getActivityByName(String merchantId, String name) {
		return activityDao.findByName(merchantId, name);
	}
	
	/**
	 * 获取当前订单匹配是否赠送商品
	 * @param sid
	 * @param amount
	 * @param orderDate
	 * @return
	 */
	public List<ActivityProduct> getActivityProducts(String sid, Double amount, Date orderDate){
		DetachedCriteria dc = activityDao.createDetachedCriteria();
		dc.add(Restrictions.like("type", "2"));
		dc.add(Restrictions.eq(Activity.FIELD_DEL_FLAG, Activity.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", sid));
		List<Activity> list = activityDao.find(dc);
		if (list.size()>0) {
			Activity activity = list.get(0);
			dc = activityTimeDao.createDetachedCriteria();
			dc.add(Restrictions.eq(Activity.FIELD_DEL_FLAG, Activity.DEL_FLAG_NORMAL));
			dc.add(Restrictions.eq("activity.id", activity.getId()));
			List<ActivityTime> times = activityTimeDao.find(dc);
			List<ActivityProduct> apList = Lists.newArrayList();
			for (ActivityTime activityTime : times) {
				if (activityTime.getType()==1) { //日周期
					Date beginTime = new Date(DateUtils.formatDate(activityTime.getActivityDate(), "yyyy/MM/dd")+" "+activityTime.getStartTime());
					Date endTime = new Date(DateUtils.formatDate(activityTime.getActivityDate(), "yyyy/MM/dd")+" "+activityTime.getEndTime());
					if(amount >= activityTime.getAmount() && beginTime.before(orderDate) && endTime.after(orderDate)){//正赠送时间内
						for (ActivityProduct activityProduct : activityTime.getProductList()) {
							apList.add(activityProduct);
						}
					}
				}else if(activityTime.getType()==2){ //周周期
					int week = DateUtils.getWeekOfDate(orderDate);
					Date endTime = new Date(DateUtils.formatDate(activityTime.getExpiryDate(), "yyyy/MM/dd")+" 23:59:59");
					if (amount >= activityTime.getAmount() && week == activityTime.getWeek() && endTime.after(orderDate)) {
						for (ActivityProduct activityProduct : activityTime.getProductList()) {
							apList.add(activityProduct);
						}
					}
				}
			}
			return apList;
		}else {
			return Lists.newArrayList();
		}
	}
}
